package com.mentalhealthplatform.schoolV1.controller;

import com.mentalhealthplatform.schoolV1.dto.TeacherDTO;
import com.mentalhealthplatform.schoolV1.service.TeacherServiceV1;
import com.mentalhealthplatform.utils.PagedResponse;
import com.mentalhealthplatform.utils.stringUtils.ConvertEmptyToNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/api/v1/teachers")
public class TeacherControllerV1 {

    @Autowired
    private TeacherServiceV1 teacherServiceV1;

    // 获取所有教师信息（分页、排序）
    @GetMapping
    public ResponseEntity<PagedResponse<TeacherDTO>> getTeachers(
            @RequestParam(required = false) @ConvertEmptyToNull String name,
            @RequestParam(required = false) @ConvertEmptyToNull String department,
            @RequestParam(required = false) @ConvertEmptyToNull String college,
            @RequestParam(required = false) @ConvertEmptyToNull String gender,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startDate,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime endDate,
            @RequestParam(defaultValue = "1") int page,          // 默认页码为1
            @RequestParam(defaultValue = "10") int pageSize,     // 默认每页大小为10
            @RequestParam(required = false) String sortField,    // 排序字段
            @RequestParam(required = false) String sortOrder) {  // 排序顺序

        // 设置分页和排序信息，确保排序字段和顺序可选
        Sort sort = Sort.by(Sort.Order.asc(sortField != null ? sortField : "id"));
        if ("desc".equalsIgnoreCase(sortOrder)) {
            sort = Sort.by(Sort.Order.desc(sortField != null ? sortField : "id"));
        }

        // 创建 Pageable 对象，包含分页和排序信息
        Pageable pageable = PageRequest.of(page - 1, pageSize, sort);

        // 调用 Service 层获取分页数据
        PagedResponse<TeacherDTO> result = teacherServiceV1.getTeachers(
                name, department, college, gender, startDate, endDate, pageable);

        return ResponseEntity.ok(result);
    }


    // 根据教师ID获取教师信息
    @GetMapping("/{teacherId}")
    public ResponseEntity<TeacherDTO> getTeacherById(@PathVariable UUID teacherId) {
        TeacherDTO teacherDTO = teacherServiceV1.getTeacherById(teacherId);
        return ResponseEntity.ok(teacherDTO);
    }

    // 根据工号获取教师信息
    @GetMapping("/number/{number}")
    public ResponseEntity<TeacherDTO> getTeacherByNumber(@PathVariable String number) {
        TeacherDTO teacherDTO = teacherServiceV1.getTeacherByNumber(number);
        return ResponseEntity.ok(teacherDTO);
    }

    // 添加新教师
    @PostMapping
    public ResponseEntity<TeacherDTO> addTeacher(@RequestBody TeacherDTO teacherDTO) {
        TeacherDTO newTeacher = teacherServiceV1.addTeacher(teacherDTO);
        return ResponseEntity.status(201).body(newTeacher);
    }

    // 更新教师信息
    @PutMapping("/{teacherId}")
    public ResponseEntity<TeacherDTO> updateTeacher(
            @PathVariable UUID teacherId, @RequestBody TeacherDTO teacherDTO) {
        TeacherDTO updatedTeacher = teacherServiceV1.updateTeacher(teacherId, teacherDTO);
        return ResponseEntity.ok(updatedTeacher);
    }

    // 删除教师
    @DeleteMapping("/{teacherId}")
    public ResponseEntity<Void> deleteTeacher(@PathVariable UUID teacherId) {
        teacherServiceV1.deleteTeacher(teacherId);
        return ResponseEntity.noContent().build();
    }

    // 批量删除教师
    @PostMapping("/delete")
    public ResponseEntity<Void> batchDeleteTeachers(@RequestBody List<UUID> teacherIds) {
        teacherServiceV1.batchDeleteTeachers(teacherIds);
        return ResponseEntity.noContent().build();
    }
}
